import 'package:cloud_medicine/components/message_user_show.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/message_api.dart';
import 'package:cloud_medicine/model/notice_message_list.dart';
import 'package:cloud_medicine/pages/post_detail.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NoticeMessageListPage extends StatefulWidget {
  @override
  _NoticeMessageListPageState createState() => _NoticeMessageListPageState();
}

class _NoticeMessageListPageState extends State<NoticeMessageListPage> {
  bool _loading = false;
  List<NoticeMessageItem> _list = [];

  @override
  void initState() {
    super.initState();
    _loadData();
  }

// 加载数据
  Future<Null> _loadData() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getNoticeMessageList_api();
    if (res['code'] == 0) {
      res['data'].forEach((dynamic noticeMessageItem) {
        NoticeMessageItem noticeMessageItemModel =
            NoticeMessageItem.init(noticeMessageItem);
        if (mounted) {
          setState(() {
            _list.add(noticeMessageItemModel);
          });
        }
      });
    } else {
      errorToast(res['message']);
    }
    setState(() {
      _loading = false;
    });
  }

  entryPostDetail(int postID) {
    Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => PostDetail(
        postID: postID,
      ),
    ));
  }

  // 移除消息
  _remove(int index, int id) async {
    setState(() {
      _list.removeAt(index);
    });;
    dynamic res = await deleteMessage_api({'id': id, 'type': 1});
    res['code'] == 0
        ? successToast(res['message'])
        : errorToast(res['message']);
  }

  @override
  Widget build(BuildContext context) {
    return _loading
        ? loadingWidget(_loading)
        : ListView.separated(
            itemCount: _list.length,
            separatorBuilder: (BuildContext context, int index) => Divider(
              indent: 70.0,
              height: 1.0,
              color: Colors.grey,
              endIndent: 10.0,
            ),
            itemBuilder: (BuildContext context, int index) {
              return Dismissible(
                background:
                    new Container(color: Theme.of(context).primaryColor),
                key: new Key(_list[index].id.toString()),
                onDismissed: (direction) {
                  _remove(index, _list[index].id);
                },
                child: Container(
                  height: 60,
                  margin: EdgeInsets.all(10),
                  child: MessageUserShow(
                    avatar: _list[index].author.avatar,
                    realname: _list[index].author.realname,
                    userID: _list[index].author.id,
                    createTime: _list[index].createTime,
                    abstract: _list[index].abstract,
                  ),
                ),
              );
            },
          );
  }
}
